package com.fivestar.pmanager.core.util;

import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;

public class CodeHelper
{

    public static boolean isNull(Object param)
    {
        return null == param;
    }

    public static boolean isNotNull(Object param)
    {
        return null != param;
    }

    public static boolean isNotNullOrEmpty(List<? extends Object> list)
    {
        return isNotNull(list) && !list.isEmpty();
    }

    public static boolean isNotNullOrEmpty(Map<? extends Object, ? extends Object> map)
    {
        return isNotNull(map) && !map.isEmpty();
    }

    public static boolean isNotNullOrEmpty(String str)
    {
        return isNotNull(str) && !str.isEmpty();
    }

    public static boolean isNotNullOrEmpty(Integer params)
    {
        return isNotNull(params) && params.intValue() > 0;
    }

    public static boolean isNotNullOrEmpty(Object[] params)
    {
        return isNotNull(params) && params.length > 0;
    }

    public static boolean isNullOrEmpty(String str)
    {
        return null == str || str.isEmpty();
    }

    public static boolean isNullOrEmpty(List<? extends Object> params)
    {
        return null == params || params.isEmpty();
    }

    public static boolean isNullOrEmpty(Object[] params)
    {
        return null == params || params.length == 0;
    }

    public static boolean isImage(String fileName)
    {
        String suffix = FilenameUtils.getExtension(fileName);
        return "png;jpg;jpeg;bmp;gif".contains(suffix);
    }

    public static boolean isVideo(String fileName)
    {
        String suffix = FilenameUtils.getExtension(fileName);
        return "video;mp4".contains(suffix);
    }

    public static boolean isRadio(String fileName)
    {
        String suffix = FilenameUtils.getExtension(fileName);
        return "sound;mp3".contains(suffix);
    }
    
    
    public static String trim(String param,String charSequence)
    {
        if(isNotNullOrEmpty(param) && isNotNullOrEmpty(charSequence))
        {
            //去除首字符
            if(param.indexOf(charSequence) == 0)
            {
                param = param.substring(1);
            }
            
            //去除尾字符
            if(param.endsWith(charSequence))
            {
                param = param.substring(0,param.length() - 1);
            } 
        }
        
        return param;
    }

    public static String getHtmlImageSize(String html,float maxWidth){

        String []result=html.split("<img");
        for(int i=0;i<result.length;i++){
            String oldHtml="<img"+result[i];
            String replaceHtml="<img"+result[i];
            String []img=result[i].split("style=");
            for(int j=0;j<img.length;j++){
                String height="0";
                String width="0";
                if(img[j].indexOf("width:")!=-1&&img[j].indexOf("px")!=-1){
                    //只有style==3才进行解析
                    if(img[j].split("px").length==3) {
                        //判断宽度和高度谁在前面
                        //宽度在前
                        if (img[j].indexOf("width:") < 6) {
                            width = img[j].substring(img[j].indexOf("width:") + 6, img[j].indexOf("px"));
                            //计算高度
                            if (img[j].indexOf("height:") != -1 && img[j].indexOf("px") != -1) {
                                height = img[j].substring(img[j].indexOf("height:") + 7, img[j].lastIndexOf("px"));
                            }
                        } else {//高度在前
                            width = img[j].substring(img[j].indexOf("width:") + 6, img[j].lastIndexOf("px"));
                            //计算高度
                            if (img[j].indexOf("height:") != -1 && img[j].indexOf("px") != -1) {
                                height = img[j].substring(img[j].indexOf("height:") + 7, img[j].indexOf("px"));
                            }
                        }
                        //判断是否符合尺寸
                        //当前图片尺寸大于移动端尺寸，执行缩放
                        if (Float.parseFloat(width) > maxWidth) {
                            //原宽度
                            float oldWidth = Float.parseFloat(width);
                            //新的高度
                            float newHeight = Float.parseFloat(height) * (maxWidth / oldWidth);

                            replaceHtml = replaceHtml.replace("width:" + width, "width:" + String.valueOf(maxWidth)).replace("height:" + height, "height:" + String.valueOf(newHeight));
                            html = html.replace(oldHtml, replaceHtml);
                        }
                    }
                }
            }
        }
        return html;
    }
    
}
